package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateAppFunctionGetPageDTO;
import com.itlong.cloud.POJO.PO.AppFunctionInfoPO;
import com.itlong.cloud.POJO.PO.AppServeProjectPO;
import com.itlong.cloud.POJO.VO.app.AppFunctionInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateAppFunctionInfoDao;
import com.itlong.cloud.operate.dao.IOperateMiniProgramFunctionDao;
import com.itlong.cloud.operate.service.IOperateAppFunctionInfoService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-APP-功能管理业务处理接口实现类
 * </desc>
 *
 * @createDate 2017/09/30
 */
@Service
public class OperateAppFunctionInfoServiceImpl implements IOperateAppFunctionInfoService {
    @Autowired
    IOperateAppFunctionInfoDao iOperateAppFunctionInfoDao;

    @Autowired
    IOperateMiniProgramFunctionDao iOperateMiniProgramFunctionDao;

    /**
     * <desc>
     *      保存功能
     * </desc>
     *
     * @param appFunctionInfoPO 功能实体
     * @return sql执行行数
     * @author Jiaqi.X
     * @createDate 2017/09/30
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer save(AppFunctionInfoPO appFunctionInfoPO) throws Exception {
        appFunctionInfoPO.setAppFunctionId(LogicIdUtil.bussinessId());
        appFunctionInfoPO.setCreateTime(new Date());
        return iOperateAppFunctionInfoDao.save(SqlUtil.durableData(appFunctionInfoPO, PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      更新功能
     * </desc>
     *
     * @param appFunctionInfoPO 功能实体
     * @return sql执行行数
     * @author Jiaqi.X
     * @createDate 2017/09/30
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer update(AppFunctionInfoPO appFunctionInfoPO) throws Exception {
        appFunctionInfoPO.setUpdateTime(new Date());
        return iOperateAppFunctionInfoDao.update(SqlUtil.durableData(appFunctionInfoPO, PlatformConstants.TABLE_UPDATE));
    }

    /**
     * <desc>
     *      通过id获取功能
     * </desc>
     *
     * @param appFunctionInfoPO 功能实体
     * @return 资源实体
     * @author Jiaqi.X
     * @createDate 2017/09/30
     */
    @Override
    public AppFunctionInfoPO getOne(AppFunctionInfoPO appFunctionInfoPO) {
        return iOperateAppFunctionInfoDao.getOne(appFunctionInfoPO);
    }

    /**
     * <desc>
     *      获取所有功能
     * </desc>
     *
     * @return 资源实体集合
     * @author Jiaqi.X
     * @createDate 2017/09/30
     */
    @Override
    public List<AppFunctionInfoPO> getList() {
        return iOperateAppFunctionInfoDao.getList();
    }

    /**
     * <desc>
     *      获取分页数据
     * </desc>
     *
     * @param pageDTO 查询参数DTO
     * @return page模型
     * @author Jiaqi.X
     * @createDate 2017/09/30
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<AppFunctionInfoPO> getPage(OperateAppFunctionGetPageDTO pageDTO) throws Exception {
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
        //分页类默认pageSize为20
        Page<AppFunctionInfoPO> page = new Page<>();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateAppFunctionInfoDao.getPage(params), iOperateAppFunctionInfoDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      删除功能
     * </desc>
     *
     * @param appFunctionIds 功能id数组
     * @return sql执行行数
     * @author Jiaqi.X
     * @createDate 2017/09/21
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer delete(String appFunctionIds) {
        String appFunctionId[] = StringHandlerUtil.splitString(appFunctionIds);
        return iOperateAppFunctionInfoDao.delete(appFunctionId, new Date());
    }

    /**
     * <desc>
     *      根据parent获取下级资源
     * </desc>
     *
     * @param appFunctionId 功能id
     * @return 子级结果集
     * @author Jiaqi.X
     * @createDate 2017/09/30
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<AppFunctionInfoVO> getChilds(String appFunctionId) {
        List<AppFunctionInfoVO> roots = iOperateAppFunctionInfoDao.getChildsByParent(appFunctionId,1);
        roots.forEach(child -> {
            List<AppFunctionInfoVO> list = iOperateAppFunctionInfoDao.getChildsByParent(child.getAppFunctionId(),1);
            if (list != null && !list.isEmpty()) {
                child.setHaveChild("true");
            }
        });
        return roots;
    }

    /**
     * <desc>
     *      获取套餐分配菜单所有节点
     * </desc>
     *
     * @param appPackageId 套餐id
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/26
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<AppFunctionInfoVO> getAllotFunction(String appPackageId) {
        List<AppFunctionInfoVO> roots = iOperateAppFunctionInfoDao.getChildsByParent("0",2);
        List<String> roleFunctions = iOperateAppFunctionInfoDao.getAppPackageFunctions(appPackageId);
        roots = recursionFunction(roots, roleFunctions);
        return roots;
    }

    /**
     * <desc>
     *      获取当前节点的父级节点以及同级别所有父级节点
     * </desc>
     *
     * @param appFunctionId 功能id
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/30
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<AppFunctionInfoPO> getParentLevelFunctions(String appFunctionId) {
        AppFunctionInfoPO appFunctionInfoPO=iOperateAppFunctionInfoDao.getParent(appFunctionId);
        List<AppFunctionInfoPO> parents=iOperateAppFunctionInfoDao.getParentLevelFunctions(appFunctionInfoPO.getFuncParentId());
        return parents;
    }

    /**
     * <desc>
     *      获取APP配置信息 供APP使用
     * </desc>
     *
     * @param projectId 项目id
     * @return
     * @author Jiaqi.X
     * @createDate 2017/11/02
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<AppFunctionInfoPO> getAppConfig(String projectId) {
        List<AppFunctionInfoPO> choiceFuncList = iOperateAppFunctionInfoDao.getAppConfig(projectId);
        if ((choiceFuncList == null||choiceFuncList.size()==0) && iOperateMiniProgramFunctionDao.getPackageCount(projectId) == 0){  //若配置的套餐为空则全量功能
            choiceFuncList = iOperateAppFunctionInfoDao.getAppList();
        }
        return choiceFuncList;
    }

    /**
     * <desc>
     *      处理旧套餐
     * </desc>
     *
     * @param
     * @return
     * @Author Zhu.yj
     * @createDate  2021/5/12
     */
    @Override
    public Integer handlerOldPackage() {
        List<String> projectIdList = iOperateAppFunctionInfoDao.getNotPackageProject();
        List<AppServeProjectPO> list = new ArrayList<>();
        for (String projectId : projectIdList){
            AppServeProjectPO appServeProjectPO = new AppServeProjectPO();
            appServeProjectPO.setProjectId(projectId);
            appServeProjectPO.setAppPackageId("1070641762520956928");
            appServeProjectPO.setStatus(DataStatusEnum.NORMAL.getType());
            appServeProjectPO.setCreateTime(new Date());
            appServeProjectPO.setUpdateTime(new Date());
            list.add(appServeProjectPO);

        }
        Integer result  = iOperateAppFunctionInfoDao.batchSaveAppServeProject(list);
        return null;
    }


    /**
     * <desc>
     *      递归获取所有节点 并查询套餐是否已有该节点
     * </desc>
     *
     * @return
     * @author Jiaqi.X
     * @createDate 2017/09/26
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<AppFunctionInfoVO> recursionFunction(List<AppFunctionInfoVO> roots, List<String> packageFunctions) {
        roots.forEach(child -> {
            List<AppFunctionInfoVO> list = iOperateAppFunctionInfoDao.getChildsByParent(child.getAppFunctionId(),2);
            //遍历roleFunctions查找第一个与child functionId相同的值，将child的haveAuth设为true
            if (packageFunctions.stream().filter(rf ->
                            rf.equals(child.getAppFunctionId())
            ).findFirst().isPresent()) {
                child.setHaveFunc("true");
            }
            if(child.getAllocation()==0){
                child.setHaveFunc("true");
            }
            if (list != null && !list.isEmpty()) {
                //遍历菜单节点与角色菜单判断是否已有
                list.forEach(function -> {
                    if (packageFunctions.stream().filter(rf ->
                                    rf.equals(function.getAppFunctionId())
                    ).findFirst().isPresent()) {
                        function.setHaveFunc("true");
                    }
                    if(function.getAllocation()==0){
                        function.setHaveFunc("true");
                    }
                });
                child.setHaveChild("true");
                child.setChilds(this.recursionFunction(list, packageFunctions));
            }
        });
        return roots;
    }
}
